<?php
/**
 * ShopEx licence
 *
 * @copyright  Copyright (c) 2005-2010 ShopEx Technologies Inc. (http://www.shopex.cn)
 * @license  http://ecos.shopex.com/license/gpl GPL License
 */
 
class tbdata_sync{
    public function __construct(){
		$this->object = kernel::single('tbdata_api_tbapi');
		$this->cat_model = app::get('tbdata')->model('cats');
		$this->prop_model = app::get('tbdata')->model('props');
		$this->prop_values_model = app::get('tbdata')->model('prop_values');
		$this->product_model = app::get('tbdata')->model('product');
	}
	public function get_props($cid,$child_path=''){
		if($child_path){
			$params['child_path'] = $child_path;
		}
		$params['cid'] = $cid;
		$params['method'] = 'taobao.itemprops.get';
		$params['fields'] = 'pid,is_input_prop,parent_pid,parent_vid,name,must,multi,prop_values,is_key_prop,is_sale_prop,is_item_prop,status,sort_order,child_template,is_allow_alias';
		$props = $this->object->get_data($params);
		if($props['itemprops_get_response']['item_props']['item_prop']){
			$prop_array = $props['itemprops_get_response']['item_props']['item_prop'];
			foreach($prop_array as $prop){
				$prop['cid'] = $cid;
				$row_exists = $this->prop_model->getList('*',array('cid'=>$cid,'pid'=>$prop['pid']),0,1);
				if($row_exists[0]){
					$prop['id'] = $row_exists[0]['id'];
				}
				$this->prop_model->save($prop);
				unset($prop['id']);
				if($prop['prop_values']['prop_value']){
					foreach($prop['prop_values']['prop_value'] as $prop_value){
						$prop_value['pid'] = $prop['pid'];
						$prop_value['cid'] = $cid;
						$prop_value['prop_name'] = $prop['name'];
						$row_exists = $this->prop_values_model->getList('*',array('cid'=>$cid,'pid'=>$prop['pid'],'vid'=>$prop_value['vid']),0,1);
						if($row_exists[0]){
							$prop_value['id'] = $row_exists[0]['id'];
						}
						$this->prop_values_model->save($prop_value);
						unset($prop_value['id']);
						if($prop_value['is_parent']){
							$_child_path = $prop['pid'].':'.$prop_value['vid'];
							$this->get_props($cid,$_child_path);
						}
					}
				}
			}
		}
	}
	public function get_products($cid,$page_no=1){
		$params['cid'] = $cid;
		$params['page_size'] = 100;
		$params['page_no'] = $page_no;
		$params['method'] = 'taobao.products.search';
		$params['fields'] = 'product_id,outer_id,created,tsc,cid,cat_name,props,props_str,binds_str,sale_props_str,collect_num,name,binds,sale_props,price,desc,pic_url,modified,product_imgs,product_prop_imgs,status,level,pic_path,vertical_market,customer_props,property_alias';
		$products = $this->object->get_data($params);
		if($products['products_search_response']['products']['product']){
			$product_array = $products['products_search_response']['products']['product'];
			foreach($product_array as $product){
				$row_exists = $this->product_model->getList('*',array('product_id'=>$product['product_id']),0,1);
				if($row_exists[0]){
					$product['id'] = $row_exists[0]['id'];
				}
				$this->product_model->save($product);
				unset($product['id']);
			}

			if($page_no>1) return true;
			$total = $products['products_search_response']['total_results'];
			if($total>$params['page_size']){
				$count = ceil($total/$params['page_size']);
				for($i=2;$i<=$count;$i++){
					$this->get_products($cid,$i);
				}
			}

		}
	}
	public function get_product_detail($product_id,$cid){
		$params['product_id'] = $product_id;
		$params['cid'] = $cid;
		$params['method'] = 'taobao.product.get';
		$params['fields'] = 'product_id,outer_id,created,tsc,cid,cat_name,props,props_str,binds_str,sale_props_str,collect_num,name,binds,sale_props,price,desc,pic_url,modified,product_imgs,product_prop_imgs,status,level,pic_path,vertical_market,customer_props,property_alias';
		$products = $this->object->get_data($params);
		if($products['product_get_response']['product']){
			$product_array = $products['product_get_response']['product'];
			$product_array['created'] = strtotime($product_array['created']);
			$product_array['modified'] = strtotime($product_array['modified']);
			$product_id = $product_array['product_id'];
			$row_exists = $this->product_model->getList('*',array('product_id'=>$product_id),0,1);
			$product_array['id'] = $row_exists[0]['id'];
			$bool = $this->product_model->save($product_array);
		}
	}
}